<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/10/25
 * Time: 14:28
 */
class ArticleCommentWechat3 extends Eloquent
{
    protected $table = "article_comment_wechat3";


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-25 15:35:28
     * @Description: 获取有效数据
     */
    public static function sortEffective($article_id,$status,$sort){
        return ArticleCommentWechat3::where('article_id',$article_id)
            ->with('parent')
            ->where('status',1)
            ->where('sort',$sort)
            ->orderBy('created_at','desc')
            ->get()
            ->toArray();
    }


    public function parent(){
        return $this->hasOne('ArticleCommentWechat3',"id","parent_id");
    }


    public static function sortEffectiveDiGui($article_id){
        $all_list = ArticleCommentWechat3::where('article_id',$article_id)
            ->where('status',1)
            //->orderBy('created_at','desc')
            ->get()
            ->toArray();
        $tmp = array();
        if($all_list){
            $v = array_column($all_list,'id');
            foreach($all_list as $key =>$val){
                $all_list[$key]['parent'] = null;
                $val['parent'] = null;
                if($val['parent_id'] != 0){
                    $s = array_search($val['parent_id'],$v);
                    if(!empty($s)){
                        $all_list[$key]['parent'] = $all_list[$s];
                        $val['parent'] = $all_list[$s];
                    }
                    if(!empty($all_list[$key]['parent'])){
                        array_push($tmp,$val);
                    }
                    if(!empty($s) && !empty($all_list[$key]['parent'])){
                        //unset($all_list[$key]);
                        //unset($v[$key]);
                        $tmp_id = array_column($tmp,'id');
                        foreach($tmp as $k1 => $v1){
                            if(!in_array($v1['parent_id'],$tmp_id) && isset($tmp[$k1]) && $v1['parent_id'] != 0){
//                                echo $v1['parent_id']. " | ";
//                                var_dump($tmp_id);
//                                var_dump($tmp[$k1]);
                                unset($tmp[$k1]);
                            }
                        }
                    }
                }else{
                    array_push($tmp,$val);
                }
            }
        }
        //die;
        return $tmp;
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-10-29 15:35:28
     * @Description: [文章评论]
     */
    public static function commentAdd($id,$content,$str,$users,$article_id,$user_type,$sort){
        //评论入库
        try{
            $comment = new ArticleCommentWechat3();

            //查询最大的楼层
            $max_floor = ArticleCommentWechat3::where('article_id',$article_id)->max('floor');
            $comment->parent_id = $id;
            $comment->floor = $max_floor + 1;
            $comment->content = $content;
            $comment->content_show = $str;
            $comment->user_id = $users->id;
            $comment->article_id = $article_id;
            $comment->status = 1;
            $comment->sort = $sort;
            $comment->Irregularities = 2;
            $comment->user_type = $user_type;
            $comment->user_phone = $users->phone;
            $comment->created_at = Common::currentTimeString();
            $comment->updated_at = Common::currentTimeString();
            $comment->save();
            return $comment;
        }catch(Exception $e){
            Log::error("error", array("system" => "api3.0" ,"event" => "articleComment", 'input' =>Input::all() ,"exception" => $e));
            $response = Lang::get('error.wechat3_article_reviews_error_20009');
        }
        return json_encode($response);
    }
}